//
//  CompleteTableController.m
//  WhatToEat
//
//  Created by Mac on 15/3/31.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "CompleteTableController.h"
#import "UUDatePicker.h"
#import "DelLetter.h"
#import "EASaveUserMessage.h"
#import "EAFoodAndWaterController.h"
#import "EATabooFoodViewController.h"
#import "EATaboolFoodCell.h"


@interface CompleteTableController ()

@property(strong,nonatomic)NSArray *pickerArray;

@property(strong,nonatomic)EAPinKerView * pinkerView;

@property(strong,nonatomic)NSDictionary * userDict;

@property(strong,nonatomic)NSMutableArray * tabooFoods;
@end


@implementation CompleteTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.navigationController.navigationBar.hidden=NO;
    [self updateUI];
    // Do any additional setup after loading the view.
    
    UITapGestureRecognizer * tp=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAllKey)];
    
    [self.view addGestureRecognizer:tp];
    
    
    self.collectionView.backgroundColor=[UIColor whiteColor];
    
    self.pinkerView=[self pinKerViews];
 
    self.tabooFoods=[@[] mutableCopy];
    
    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
 
    

    [self.collectionView registerNib:[UINib nibWithNibName:@"EATaboolFoodCell" bundle:nil] forCellWithReuseIdentifier:@"EATaboolFoodCell"]; 
    
    

    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"保存" style:0 target:self action:@selector(okAction)];
    
    

}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    

    
 
    

    


    
    if (self.userDict) {

        
        self.sexyTF.text=self.userDict[@"sex"]?@"男":@"女";;
        self.ageTF.text=self.userDict[@"birthday"];
        self.weightTF.text=self.userDict[@"weight"];
        self.heightTF.text=self.userDict[@"height"];
    }
    NSLog(@"88888888%@",ApplicationDelegate.userDictionary);
    
    if (ApplicationDelegate.userDictionary[@"taboo"]) {
        
        
            self.tabooFoods=[ApplicationDelegate.userDictionary[@"taboo"] mutableCopy];
    }
    
   [self.collectionView reloadData];
}


-(void)updateUI
{
    

    UIBarButtonItem *bt=[[UIBarButtonItem alloc] init];
    bt.title=@"返回";
    self.navigationItem.backBarButtonItem=bt;
    
    if (_isInformation) {
        
    self.navigationItem.title=@"个人信息";
    
        self.okBtn.hidden=YES;
        [self.navigationItem setHidesBackButton:NO];
        

        self.sexyTF.text=ApplicationDelegate.userDictionary[@"sex"]?@"男":@"女";
        self.ageTF.text=ApplicationDelegate.userDictionary[@"birthday"];
        self.weightTF.text=[NSString stringWithFormat:@"%@kg",ApplicationDelegate.userDictionary[@"weight"]];
        self.heightTF.text=[NSString stringWithFormat:@"%@cm",ApplicationDelegate.userDictionary[@"height"]];
        
        
        NSLog(@"用户信息------%@",ApplicationDelegate.userDictionary);
    
        
        
        
    }else
    {
      self.navigationItem.title=@"完善资料";
      self.okBtn.hidden=NO;
        
        [self.navigationItem setHidesBackButton:YES];
        self.navigationItem.leftBarButtonItem=nil;
    }
    
    

    self.tableView.backgroundColor=kWhiteColor;

    
    self.okBtn.layer.cornerRadius=kRadius;
    self.okBtn.clipsToBounds=YES;
    
    
    self.view.backgroundColor=kWhiteColor;
    
    self.pickerArray=@[@"男",@"女"];
    
    
    
    

}


- (void)setExtraCellLineHidden: (UITableView *)tableView{
    
    UIView *view =[ [UIView alloc]init];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
    
    [tableView setTableHeaderView:view];
    

}


//＝完善资料接口=
//URL：　http://120.24.212.47/api/user/profile, 参数你知道的
//返回:
//1: 错误返回 code = 9999, error = unknown error
//2:　成功返回 code = 1, user 是用户信息


- (IBAction)okAction
{
    if (self.sexyTF.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请选择性别"];
        return;
    }
    
    if (self.ageTF.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请选择出身日期"];
        return;
    }
    
    if (self.weightTF.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请选择体重"];
        return;
    }
    
    if (self.heightTF.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请选择身高"];
        return;
    }
    
    
    NSString *sexStr;
    if ([self.sexyTF.text isEqualToString:@"男"]) {
        sexStr=@"1";
        
    }

    if ([self.sexyTF.text isEqualToString:@"女"]) {
        sexStr=@"0";
        
    }
    
    
    
    NSString * arrString=[self.tabooFoods JSONString];

    
    
    self.userDict=@{
                    @"userID":ApplicationDelegate.userId,
                    
                    @"sex":sexStr,
                    
                    @"birthday":self.ageTF.text,
                    
                    @"weight":self.weightTF.text,
                    
                    @"height":self.heightTF.text,
                    
                    @"taboo":arrString
                    
                    };
    

    [WebAPI webAPISignAddMd5tWithNote:@"user/profile" parm:self.userDict completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
        
        
        
                                                                if ([responseObject[@"code"] isEqualToString:@"1"]) {
                                                         
                                                                    
                                                                    [EASaveUserMessage saveUserMessage:responseObject andView:nil];
                                                                    
                                                                    if (_isInformation) {
                                                                    
                                                                        [self.navigationController popViewControllerAnimated:YES];
                                                                    
                                                                    }else{
                                                   

                                                                UIViewController * vc=[ApplicationDelegate.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"MainViewControllerNV"];
                                                                    
                                                                    
                                                                    [self presentViewController:vc animated:YES completion:^{
                                                                        
                                                                    }];
                                                                    
                                                                }                                                    
                                                                
                                                            }
    }];
    
    
    
    
    
    
    
    

}

-(void)viewWillDisappear:(BOOL)animated
{

    [super viewWillAppear:animated];
    
    NSString *sexStr;
    if ([self.sexyTF.text isEqualToString:@"男"]) {
        sexStr=@"1";
        
    }
    
    if ([self.sexyTF.text isEqualToString:@"女"]) {
        sexStr=@"0";
        
    }

    self.userDict=@{
                    @"userID":ApplicationDelegate.userId,
                    
                    @"sex":sexStr,
                    
                    @"birthday":self.ageTF.text,
                    
                    @"weight":self.weightTF.text,
                    
                    @"height":self.heightTF.text
                    
                    };
    
    [self cancelAllKey];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    
    
            __weak typeof(self) weakSelf=self;
    
    

    
    switch (textField.tag) {
        case 0:{
            
            
            self.pinkerView.titleLabel.text=@"请选择你的性别";
            
           self.pickerArray=@[@"男",@"女"];

            
            textField.inputView=self.pinkerView;
   
        
            self.pinkerView.okBlock=^(UIButton * bt)
            {
            
                textField.text=weakSelf.pickerArray[ [weakSelf.pinkerView.pickerView selectedRowInComponent:0]];
                
                [self cancelAllKey];
                
            };
            
            
            
        }
            
            break;
        case 1:
            
        {

            
            UIView * bgView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_SCREEN_WIDTH, 240)];
            
            
            

            UUDatePicker *picker=[[UUDatePicker alloc] initWithframe:CGRectMake(0, 40, kUI_SCREEN_WIDTH, 180) PickerStyle:1 didSelected:^(NSString *year, NSString *month, NSString *day, NSString *hour, NSString *minute, NSString *weekDay) {
                
                textField.text = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
                
                

            }];
            
            [bgView addSubview:picker];
            
         
            NSString *stringDate = @"01/15/1990";
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"MM/dd/yyyy"];
            NSDate *dateCheck = [dateFormatter dateFromString:stringDate];
            
            
            picker.ScrollToDate=dateCheck;
            
            
            
            if (textField.text.length>3) {
                
         
            
            
            NSString * textString=textField.text;
            
            
            @try {
                
                
                NSDateFormatter * dateFormatter1=[[NSDateFormatter alloc] init];
                [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
                NSDate * date1=[dateFormatter1 dateFromString:textString];
                
                picker.ScrollToDate=date1;
                
                
                
                
            }
            @catch (NSException *exception) {
                
            }
            @finally {
                
            }
            }else
            {
            
                NSDateFormatter * dateFormatter1=[[NSDateFormatter alloc] init];
                [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];

                textField.text=[dateFormatter stringFromDate:dateCheck];
            
            
            
            }

           
            
            
            
            
            UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(18, 0, 180, 40)];
            l.text=@"请选择出生日期";
            l.backgroundColor=[UIColor whiteColor];
            l.textColor=[UIColor blackColor];

            l.textAlignment=0;
            [bgView addSubview:l];
       
            UIButton *bt=[[UIButton alloc] initWithFrame:CGRectMake(kUI_SCREEN_WIDTH-60, 5, 50, 30)];
            [bt setTitle:@"完成" forState:0];
        
            [bt setTitleColor:kTingColor forState:0];
            bt.backgroundColor=[UIColor clearColor];
            
            [bt addTarget:self action:@selector(hiddenDatePicker) forControlEvents:UIControlEventTouchUpInside];
            bt.userInteractionEnabled=YES;
            [bgView addSubview:bt];
            
            textField.inputView=bgView;
            textField.inputView.backgroundColor=[UIColor whiteColor];
            
            

            
        }
            break;
        case 2:{
            self.pickerArray=@[@"120cm",@"121cm",@"122cm",@"123cm",@"124cm",@"125cm",@"126cm",@"127cm",@"128cm",@"129cm",@"130cm",@"131cm",@"132cm",@"133cm",@"134cm",@"135cm",@"136cm",@"137cm",@"138cm",@"139cm",@"140cm",@"141cm",@"142cm",@"143cm",@"144cm",@"145cm",@"146cm",@"147cm",@"148cm",@"149cm",@"150cm",@"151cm",@"152cm",@"153cm",@"154cm",@"155cm",@"156cm",@"157cm",@"158cm",@"159cm",@"160cm",@"161cm",@"162cm",@"163cm",@"164cm",@"165cm",@"166cm",@"167cm",@"168cm",@"169cm",@"170cm",@"171cm",@"172cm",@"173cm",@"174cm",@"175cm",@"176cm",@"177cm",@"178cm",@"179cm",@"180cm",@"181cm",@"182cm",@"183cm",@"184cm",@"185cm",@"186cm",@"187cm",@"188cm",@"189cm",@"190cm",@"191cm",@"192cm",@"193cm",@"194cm",@"195cm",@"196cm",@"197cm",@"198cm",@"199cm",@"200cm"];
            
            
            
            
            textField.inputView=self.pinkerView;
            
            
            __weak typeof(self) weakSelf=self;
            
            
            self.pinkerView.okBlock=^(UIButton * bt)
            {
                
                textField.text=weakSelf.pickerArray[ [weakSelf.pinkerView.pickerView selectedRowInComponent:0]];
                
                [weakSelf cancelAllKey];
                
            };
            
            
            NSString * w=[NSString stringWithFormat:@"%@cm",ApplicationDelegate.userDictionary[@"height"]];
            
            @try {
                NSInteger  iw=[self.pickerArray indexOfObject:w];
                [self.pinkerView.pickerView selectRow:iw inComponent:0 animated:YES];
            }
            @catch (NSException *exception) {
                
            }
            @finally {
                
            }
            
            
            
            
            
        }
            break;
        case 3:
        {
            self.pickerArray=@[@"30.0kg",@"30.5kg",@"31.0kg",@"31.5kg",@"32.0kg",@"32.5kg",@"33.0kg",@"33.5kg",@"34.0kg",@"34.5kg",@"35.0kg",@"35.5kg",@"36.0kg",@"36.5kg",@"37.0kg",@"37.5kg",@"38.0kg",@"38.5kg",@"39.0kg",@"39.5kg",@"40.0kg",@"40.0kg",@"40.5kg",@"41.0kg",@"41.5kg",@"42.0kg",@"42.5kg",@"43.0kg",@"43.5kg",@"44.0kg",@"44.5kg",@"45.0kg",@"45.5kg",@"46.0kg",@"46.5kg",@"47.0kg",@"47.5kg",@"48.0kg",@"48.5kg",@"49.0kg",@"49.5kg",@"50.0kg",@"50.0kg",@"50.5kg",@"51.0kg",@"51.5kg",@"52.0kg",@"52.5kg",@"53.0kg",@"53.5kg",@"54.0kg",@"54.5kg",@"55.0kg",@"55.5kg",@"56.0kg",@"56.5kg",@"57.0kg",@"57.5kg",@"58.0kg",@"58.5kg",@"59.0kg",@"59.5kg",@"60.0kg",@"60.0kg",@"60.5kg",@"61.0kg"];
            
            
            __weak typeof(self) weakSelf=self;
            textField.inputView=self.pinkerView;
            
            self.pinkerView.okBlock=^(UIButton * bt)
            {
                
                textField.text=weakSelf.pickerArray[ [weakSelf.pinkerView.pickerView selectedRowInComponent:0]];
                
                
                
                [weakSelf  cancelAllKey];
                
            };
            
            @try {
                NSString * w=[NSString stringWithFormat:@"%@kg",ApplicationDelegate.userDictionary[@"weight"]];
                NSInteger  iw=[self.pickerArray indexOfObject:w];
                
                [self.pinkerView.pickerView selectRow:iw inComponent:0 animated:YES];
            }
            @catch (NSException *exception) {
                
            }
            @finally {
                
            }
            
            
            
            
            
        }
        
            break;
            
            case 4:
            
        {
            EATabooFoodViewController * eaVC=[[EATabooFoodViewController alloc] init];
            
            
            NSLog(@"%@",ApplicationDelegate.tabooFoods);
            
            self.hidesBottomBarWhenPushed=YES;
            
            [self.navigationController pushViewController:eaVC animated:YES];
            
            self.hidesBottomBarWhenPushed=YES;
            return NO;
        
        
            
            
            
        }
            
            
            
            
            
            
            break;
        default:
            break;
    }



    UIView *subView=textField.superview;
    NSArray *arr=subView.subviews;
    

    

    for (UITableViewCell *cell in self.tableView.visibleCells) {
        
        UIView *contentView=cell.contentView;
        
        for (UIView * v in contentView.subviews) {
            
            if ([v respondsToSelector:@selector(setTextColor:)]) {
                
                [v setValue:[UIColor darkGrayColor] forKey:@"textColor"];
                
            }
            
            
            if ([v respondsToSelector:@selector(setImage:)]) {
                [v setValue:[UIImage imageNamed:@"xuanze1"] forKey:@"image"];
            }
            
            
            
            
        }
        
        
        for (UIView *v in arr) {
            
            if ([v respondsToSelector:@selector(setTextColor:)]) {
                
                [v setValue:kTingColor forKey:@"textColor"];
                
            }
            
            
            if ([v respondsToSelector:@selector(setImage:)]) {
                [v setValue:[UIImage imageNamed:@"xuanze2"] forKey:@"image"];
            }
            
            
            
        }

        
        
        
        
        
    }
    

    
    [self.pinkerView.pickerView reloadAllComponents];
    
    
       return YES;

    




}






-(EAPinKerView *)pinKerViews
{
    
    EAPinKerView * pinKerView=[[NSBundle mainBundle] loadNibNamed:@"EAPinKerView" owner:nil options:nil][0];
    
    pinKerView.pickerView.delegate=self;
    
    pinKerView.pickerView.dataSource=self;
    
    
    
    


    
    
    
    
    
    
    

    return pinKerView;


}





#pragma mark----pinker--代理

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.pickerArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [self.pickerArray objectAtIndex:row];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{

    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    NSString *m=self.pickerArray[row];


//    switch (pickerView.tag) {
//        case 0:
//            self.sexyTF.text=m;
//            [self.sexyTF resignFirstResponder];
//            
//            break;
//        case 1:
//    self.ageTF.text=m;
//            [self.ageTF resignFirstResponder];
//            
//            break;
//        case 2:
//            self.heightTF.text=m;
//            [self.heightTF resignFirstResponder];
//            
//            break;
//        case 3:
//            self.weightTF.text=m;
//            [self.weightTF resignFirstResponder];
//            
//            break;
//            
//        default:
//            break;
//    }
    

    
}



-(void)hiddenDatePicker
{

    [self.ageTF resignFirstResponder];

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

    NSLog(@"%lu",buttonIndex);

    if (buttonIndex==0) {
        self.sexyTF.text=@"男";
        return;
    }
    
    if (buttonIndex==1) {
        self.sexyTF.text=@"女";
        
        return;
    }
    
    
    
    
}


-(void)cancelAllKey
{

    [self.sexyTF resignFirstResponder];
    
    [self.ageTF resignFirstResponder];
    
    [self.weightTF resignFirstResponder];
    
    [self.heightTF resignFirstResponder];
    


}



-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
 
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    
    NSLog(@"%@",ApplicationDelegate.userDictionary);
    
    
    
    NSMutableArray * arr=self.tabooFoods;
    
    
    
    
    return arr.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    EATaboolFoodCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"EATaboolFoodCell" forIndexPath:indexPath];
    
    
    NSMutableArray * arr=self.tabooFoods;
    
 
    
   // NSMutableArray * arr=self.tabooFoods[indexPath.section];
    
    
    cell.foodNameLabel.text=arr[indexPath.row];
    
    cell.tag=indexPath.row;
    
    
    
    if (indexPath.section==1) {
        
        
        cell.imageV.hidden=YES;
        
    }else
    {
        cell.imageV.hidden=NO;
        
    }
    
    
    
    
    cell.tapAction=^(UITapGestureRecognizer * tap){
        
        CGPoint p =[tap locationInView:tap.view];
        
        if (indexPath.section==0) {
            if (CGRectContainsPoint(CGRectMake(60,0, 30, 30), p) ) {
                
                
   
                
                
                
                
                
                
                 [self.tabooFoods removeObjectAtIndex:indexPath.row];
                [UIView animateWithDuration:1 animations:^{
                    [self.collectionView reloadData];
                }];
                
                
                
            }
            
        }
        
        
        
        NSLog(@"%@",tap);
        
        
    };
    
    return cell;
    
}

@end
